﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using ProjectStorage.DocumentGenerator;

namespace StorageProject.DocumentGenerator.Transformations
{
    public class Transformer
    {
        public Stream LoadFile(String fileName)
        {
            var fs = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(fileName);
            return fs;
        }

        public System.IO.TextReader Transform(String fileName, TextReader reader)
        {
            System.Xml.Xsl.XslTransform tranform = new System.Xml.Xsl.XslTransform();
            tranform.Load(XmlReader.Create(LoadFile(fileName)));

            var doc = new XPathDocument(reader);

            var ms = new MemoryStream();
            TextWriter writer = new StreamWriter(ms);

            tranform.Transform(doc.CreateNavigator(), new System.Xml.Xsl.XsltArgumentList(), writer);

            ms.Position = 0;

            return new StreamReader(ms);
        }
        public System.IO.TextReader Transform(String fileName, Object obj)
        {
            return Transform(fileName, Common.ObjectToReader(obj));
        }
    }
}
